// En2CnMFCDlg.cpp : implementation file
//

#include "stdafx.h"
#include "En2CnMFC.h"
#include "En2CnMFCDlg.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

t_firstCharOffset g_firstCharOffset[]=
{
	'a',0, 
		'b',2873,
		'c',5360,
		'd',10066,
		'e',13075,
		'f', 15106,
		'g',17073,
		'h',18577,
		'i',20255,
		'j',22426,
		'k',22825,
		'l',23210,
		'm',24689,
		'n',27274,
		'o',28289,
		'p',29457,
		'q',33515,
		'r',33766,
		's',36057,
		't',40035,
		'u',40952,
		'v',41907,
		'w',42789,
		'x',44060,
		'y',44103,
		'z',44236,
		'E',ALL_WORDS_NUM
};

/////////////////////////////////////////////////////////////////////////////
// CAboutDlg dialog used for App About

class CAboutDlg : public CDialog
{
public:
	CAboutDlg();

// Dialog Data
	//{{AFX_DATA(CAboutDlg)
	enum { IDD = IDD_ABOUTBOX };
	//}}AFX_DATA

	// ClassWizard generated virtual function overrides
	//{{AFX_VIRTUAL(CAboutDlg)
	protected:
	virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support
	//}}AFX_VIRTUAL

// Implementation
protected:
	//{{AFX_MSG(CAboutDlg)
	//}}AFX_MSG
	DECLARE_MESSAGE_MAP()
};

CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
{
	//{{AFX_DATA_INIT(CAboutDlg)
	//}}AFX_DATA_INIT
}

void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CAboutDlg)
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
	//{{AFX_MSG_MAP(CAboutDlg)
		// No message handlers
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CEn2CnMFCDlg dialog

CEn2CnMFCDlg::CEn2CnMFCDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CEn2CnMFCDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CEn2CnMFCDlg)
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CEn2CnMFCDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CEn2CnMFCDlg)
	DDX_Control(pDX, IDC_EDIT_INPUT_WORD, m_textInputWord);
	DDX_Control(pDX, IDC_LIST_WORDS, m_listWords);
	DDX_Control(pDX, IDC_EDIT_RESULT, m_textResult);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CEn2CnMFCDlg, CDialog)
	//{{AFX_MSG_MAP(CEn2CnMFCDlg)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDC_BUTTON_OK, OnButtonOk)
	ON_EN_CHANGE(IDC_EDIT_INPUT_WORD, OnChangeEditInputWord)
	ON_LBN_SELCHANGE(IDC_LIST_WORDS, OnSelchangeListWords)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CEn2CnMFCDlg message handlers

BOOL CEn2CnMFCDlg::OnInitDialog()
{
	CDialog::OnInitDialog();

	// Add "About..." menu item to system menu.

	// IDM_ABOUTBOX must be in the system command range.
	ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
	ASSERT(IDM_ABOUTBOX < 0xF000);

	CMenu* pSysMenu = GetSystemMenu(FALSE);
	if (pSysMenu != NULL)
	{
		CString strAboutMenu;
		strAboutMenu.LoadString(IDS_ABOUTBOX);
		if (!strAboutMenu.IsEmpty())
		{
			pSysMenu->AppendMenu(MF_SEPARATOR);
			pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
		}
	}

	// Set the icon for this dialog.  The framework does this automatically
	//  when the application's main window is not a dialog
	SetIcon(m_hIcon, TRUE);			// Set big icon
	SetIcon(m_hIcon, FALSE);		// Set small icon
	
	// TODO: Add extra initialization here
	u8 ret = 0;

	memset(g_AllEnWords, 0x00, sizeof(g_AllEnWords));
	memset(g_AllExplanation, 0x00, sizeof(g_AllExplanation));
	
	ret = initLib(LIB_TYPE_WORDS);
	if (ret)
	{
		AfxMessageBox("Init Lib word_lib.h failed", MB_OK, 0);
	}
	ret = initLib(LIB_TYPE_CN_EXPLANATION);
	if (ret)
	{
		AfxMessageBox("Init Lib cn_explanation_lib.h failed", MB_OK, 0);
	}
	
	return TRUE;  // return TRUE  unless you set the focus to a control
}

void CEn2CnMFCDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
	if ((nID & 0xFFF0) == IDM_ABOUTBOX)
	{
		CAboutDlg dlgAbout;
		dlgAbout.DoModal();
	}
	else
	{
		CDialog::OnSysCommand(nID, lParam);
	}
}

// If you add a minimize button to your dialog, you will need the code below
//  to draw the icon.  For MFC applications using the document/view model,
//  this is automatically done for you by the framework.

void CEn2CnMFCDlg::OnPaint() 
{
	if (IsIconic())
	{
		CPaintDC dc(this); // device context for painting

		SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);

		// Center icon in client rectangle
		int cxIcon = GetSystemMetrics(SM_CXICON);
		int cyIcon = GetSystemMetrics(SM_CYICON);
		CRect rect;
		GetClientRect(&rect);
		int x = (rect.Width() - cxIcon + 1) / 2;
		int y = (rect.Height() - cyIcon + 1) / 2;

		// Draw the icon
		dc.DrawIcon(x, y, m_hIcon);
	}
	else
	{
		CDialog::OnPaint();
	}
}

// The system calls this to obtain the cursor to display while the user drags
//  the minimized window.
HCURSOR CEn2CnMFCDlg::OnQueryDragIcon()
{
	return (HCURSOR) m_hIcon;
}

void CEn2CnMFCDlg::OnButtonOk() 
{
	// TODO: Add your control notification handler code here
	
}

void CEn2CnMFCDlg::OnChangeEditInputWord() 
{
	// TODO: If this is a RICHEDIT control, the control will not
	// send this notification unless you override the CDialog::OnInitDialog()
	// function and call CRichEditCtrl().SetEventMask()
	// with the ENM_CHANGE flag ORed into the mask.
	
	// TODO: Add your control notification handler code here
	m_listWords.ResetContent();
	m_textResult.SetWindowText("");
	u16 firstIndex = 0, i = 0;

	CString cstrInputWord;
	m_textInputWord.GetWindowText(cstrInputWord);
	if (cstrInputWord == "")
	{
		return;
	}
	cstrInputWord.MakeLower();

	firstIndex = findInLib((LPSTR)(LPCTSTR)cstrInputWord);
	if(firstIndex >= 0)
	{
		m_textResult.SetWindowText(g_AllExplanation[firstIndex].explanation);
		for (i=firstIndex; i<ALL_WORDS_NUM; i++)
		{
			if (strncmp((LPSTR)(LPCTSTR)cstrInputWord, g_AllEnWords[i].word, strlen((LPSTR)(LPCTSTR)cstrInputWord)) == 0)
			{
				m_listWords.AddString(_T(g_AllEnWords[i].word));
			} 
			else
			{
				break;
			}
		}
	}
	else
	{
		AfxMessageBox("Not Find", MB_OK, 0);
	}

}

int CEn2CnMFCDlg::initLib(u8 libType)
{
	u16 lineLen = 0;
	u32 size = (libType == LIB_TYPE_WORDS?ALL_WORDS_NUM:ALL_EXPLANATION_NUM);
	u32 count = 0;
	FILE *fp = NULL;
	char lineBuf[MAX_EXPLANATION_LEN+1];
	
	if (libType == LIB_TYPE_WORDS)
	{
		fp = fopen(LIB_PATH_EN_WORDS, "r");
	} 
	else if (libType == LIB_TYPE_CN_EXPLANATION)
	{
		fp = fopen(LIB_PATH_CN_EXPLANATION, "r");
	} 
	else
	{
		fp = fopen(LIB_PATH_EN_EXPLANATION, "r");
	}
	
	if(NULL == fp)
	{
		printf("Open file %s failed\n", libType==LIB_TYPE_WORDS?LIB_PATH_EN_WORDS: \
			(libType==LIB_TYPE_CN_EXPLANATION?LIB_PATH_CN_EXPLANATION:LIB_PATH_EN_EXPLANATION));
		return -1;
	}
	while(fgets(lineBuf, MAX_EXPLANATION_LEN, fp))
	{
		if(lineBuf[0] != '"')
		{
			continue;
		}
		
		lineLen = strlen(lineBuf)-3;
		lineBuf[lineLen] = '\0';
		strlwr(lineBuf);
		
		if(libType == LIB_TYPE_WORDS)
		{
			g_AllEnWords[count].word = (char *)malloc(lineLen+1);
			strncpy(g_AllEnWords[count].word, lineBuf+1, lineLen);
		}
		else
		{
			g_AllExplanation[count].explanation = (char *)malloc(lineLen+1);
			strncpy(g_AllExplanation[count].explanation, lineBuf+1, lineLen);
		}
		count++;
		if (count > size)
		{
			printf("The end of array\n");
			break;
		}
		//printf("%s", lineBuf);
	}
	fclose(fp);
	
	return 0;
}

int CEn2CnMFCDlg::cleanLib(u8 libType)
{
	u32 i = 0;
	u32 size = (libType == LIB_TYPE_WORDS?ALL_WORDS_NUM:ALL_EXPLANATION_NUM);
	
	for (i=0; i<size; i++)
	{
		if(libType == LIB_TYPE_WORDS)
		{
			if(NULL != g_AllEnWords[i].word)
			{
				free(g_AllEnWords[i].word);
				g_AllEnWords[i].word = NULL;
			}
		}
		else
		{
			if(NULL != g_AllExplanation[i].explanation)
			{
				free(g_AllExplanation[i].explanation);
				g_AllExplanation[i].explanation = NULL;
			}
		}
	}
	
	return 0;
}

int CEn2CnMFCDlg::findInLib(const char *word)
{
	char firstChar;
	u16 lowIndex =0, midIndex = 0, highIndex = 0;
	u16 i = 0;
	
	firstChar = word[0];
	lowIndex = g_firstCharOffset[firstChar-'a'].offset;
	highIndex = g_firstCharOffset[firstChar-'a'+1].offset;
	
	for (i = lowIndex; i < highIndex; i++)
	{
		if (strncmp(word, g_AllEnWords[i].word, strlen(word)) == 0)
		{
			printf("%s:\n", word);
			printf("%s\n", g_AllExplanation[i].explanation);
			return i;
		}
	}
	
	return -1;
}

void CEn2CnMFCDlg::OnSelchangeListWords() 
{
	// TODO: Add your control notification handler code here
	m_textResult.SetWindowText("");
	u16 firstIndex = 0;
	
	CString cstrCurSelWord;
	m_listWords.GetText(m_listWords.GetCurSel(), cstrCurSelWord);
	
	firstIndex = findInLib((LPSTR)(LPCTSTR)cstrCurSelWord);
	if (firstIndex >= 0)
	{
		m_textResult.SetWindowText(g_AllExplanation[firstIndex].explanation);
	} 
	else
	{
		AfxMessageBox("Not Find", MB_OK, 0);
	}
	
}
